async function sendMessage() {
    const button = document.getElementById('send-button');
    const input = document.getElementById('user-input');
    
    // 如果按钮已经在加载状态或输入为空，则返回
    if (button.classList.contains('loading') || !input.value.trim()) {
        return;
    }
    
    // 添加加载状态
    button.classList.add('loading');
    button.disabled = true;
    
    const message = input.value.trim();
    
    appendMessage('user', message);
    input.value = '';
    
    try {
        const response = await fetch('/api/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ message: message })
        });
        
        const data = await response.json();
        appendMessage('assistant', data.response);
    } catch (error) {
        console.error('发送消息出错:', error);
        appendMessage('assistant', '抱歉，发生了错误');
    } finally {
        // 恢复按钮状态
        button.classList.remove('loading');
        button.disabled = false;
    }
}

function appendMessage(role, content) {
    const messagesDiv = document.getElementById('chat-messages');
    const messageDiv = document.createElement('div');
    messageDiv.className = `message ${role}`;
    messageDiv.textContent = `${role === 'user' ? '你' : 'DeepSeek'}: ${content}`;
    messagesDiv.appendChild(messageDiv);
    messagesDiv.scrollTop = messagesDiv.scrollHeight;
} 